package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.sgb.link.entity.merchant.SelfProjectList;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.merchant.view.activity.SelfProjectListActivity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SelfProjectViewModel extends BaseViewModel {
    public MutableLiveData<List<SelfProjectList>> liveDataInvestment = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldDel=new MutableLiveData<>();
    public MutableLiveData<Boolean> ldState=new MutableLiveData<>();
    public MutableLiveData<Boolean> ldStatus=new MutableLiveData<>();
    private int pageNum = 1;

    public SelfProjectViewModel(@NonNull @NotNull Application application) {
        super(application);
    }
    public void getInvestmentData(boolean isLoadMore, Map<String, Object> map) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
        map.put("limit", 20);
        MerchantsNet.getInstance().getService().getSelfProject(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<SelfProjectList>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<SelfProjectList>>> data) {
                if(data.isSuccess()){
                    liveDataInvestment.postValue(data.getData().getList());
                }else{
                    liveDataInvestment.postValue(new ArrayList<SelfProjectList>());
                }
            }
        });
    }
    public void delProject(long no){
        MerchantsNet.getInstance().getService().delProject(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    MToast.showToast(getApplication(),"删除成功");
                    ldDel.postValue(true);
                }
            }
        });
    }
    /**
     * 获取详情页并进行状态判断
     * @param no
     */
    public void getDetailState(Context context,Long no, int type){//0 编辑 1上架 2下架 3删除

        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data.isSuccess()) {
                    int status=data.getData();//0已下架 1已删除 2已上架
                    if(status==1){
                        MToast.showToast(context, "当前数据已删除");
                        ldStatus.postValue(true);
                        return;
                    }
                    switch (type){
                        case 0:
                            if(status!=0){
                                ldStatus.postValue(false);
                            }else{
                               getDetail(no);
                            }
                            break;
                        case 1:
                            if(status!=0){
                                ldStatus.postValue(false);
                            }else{
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("确定上架该条信息？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 ->{updateInvestment(no,"0");}).show();
                            }
                            break;
                        case 2:
                            if(status!=2){
                                ldStatus.postValue(false);
                            }else{
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("下架该信息后，前台将不再继续展示，确定下架？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 ->{updateInvestment(no,"1");}).show();
                            }
                            break;
                        case 3:
                            if(status!=0){
                                ldStatus.postValue(false);
                            }else{
                                new RoundDialog.Builder(context)
                                        .title("温馨提示")
                                        .content("请问是否确认删除？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .cancelable(false)
                                        .confirmCallback(v12 ->{delProject(no);}).show();
                            }
                            break;
                    }


                }
            }
        });
    }

    /**
     * 上架下架操作
     * @param no
     * @param status 0上架 1下架
     */
    public void updateInvestment(long no,String status){
        MerchantsNet.getInstance().getService().updateInvestment(no,status).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if(data.isSuccess()){
                    ldState.postValue(true);
                }
            }
        });
    }

    /****
     * 获取详情
     * @param no 项目no
     */
    public void getDetail(Long no) {
        MerchantsNet.getInstance().getService().getInvestmentDetail(no).enqueue(new ICallBack<BaseEntity<InvestmentDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<InvestmentDetailEntity> data) {
                if (data == null) {
                    return;
                }

                if (data.getData() != null) {
                    ARouter.getInstance().build(PathMerchant.InvestmentPublishActivity).withSerializable(RouterKey.DATA,data.getData())
                            .withBoolean(RouterKey.BOOLEAN,true).navigation();
                }

            }
        });
    }
}
